package com.itheima.spider.version2;


import com.google.gson.Gson;
import com.itheima.spider.pojo.News;
import com.itheima.spider.utils.FinalRedisKeyUtils;
import com.itheima.spider.utils.HttpClientUtils;
import com.itheima.spider.utils.IdWorker;
import com.itheima.spider.utils.JedisUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.util.List;

public class News163Slave {
    //从redis中获取详情页的url  ,发送请求获取数据 ,解析新闻详情页 得到news对象
    //将news对象转换为json ,将这个newsjson保存到redis中list中 ,循环获取下一页的url
    public static void main(String[] args) throws IOException {
        while (true) {
            Jedis jedis = JedisUtils.getJedis();
            List<String> list = jedis.blpop(20, FinalRedisKeyUtils.ITEM_URL_REDIS_LIST);

            jedis.close();
            if(list==null||list.size()==0){
                break;
            }
            String docurl = list.get(1);
            //发送请求 解析详情页的数据
            News news = parseItemNews(docurl);
            //将news对象转成json数据
            Gson gson = new Gson();
            String newsJson = gson.toJson(news);
            //将newsJson保存到redis的list中
            jedis = JedisUtils.getJedis();
            jedis.lpush(FinalRedisKeyUtils.NEWS_JSON_REDIS_LIST,newsJson);
            jedis.close();
        }

    }

    private static News parseItemNews(String docurl) throws IOException {
        //发送请求获取数据
        String html = HttpClientUtils.doGet(docurl);
        //通过jsoup解析新闻数据的html
        Document document = Jsoup.parse(html);
        //将解析的新闻数据封装到News对象
        News news = new News();
        //新闻的ID
        IdWorker idWorker = new IdWorker(2, 4);
        long id = idWorker.nextId();
        news.setId(id + "");

        //新闻的标题
        Elements titleEl = document.select("#epContentLeft>h1");
        String title = titleEl.text();

        news.setTitle(title);
        //新闻的时间
        // 2019-06-08 10:20:53　来源: <a id="ne_article_source" href="#" target="_blank">网易娱乐</a>
        Elements timeandsourceEl = document.select(".post_time_source");
        String timeandsource = timeandsourceEl.text();
        String[] split = timeandsource.split("　");
        news.setTime(split[0]);
        //新闻的来源
        Elements sourceEl = document.select("#ne_article_source");
        String source = sourceEl.text();
        news.setSource(source);
        //新闻的正文
        Elements textEl = document.select("#endText p");
        String text = textEl.text();
        news.setContent(text);
        //新闻的编辑
        // 责任编辑：陈少杰_b6952
        Elements editorEl = document.select(".ep-editor");
        String editor = editorEl.text();
        editor.substring(editor.indexOf(":") + 1, editor.indexOf("_"));

        news.setEditor(editor);
        //新闻的docurl
        news.setDocurl(docurl);


        //

        return news;


    }


}
